package D12_6z.反射和IO_1;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Demo1 {
    public static void main(String[] args) throws Exception {
        System.out.println(list());
    }
    public static List<Animal> list() throws Exception {
        ArrayList<Animal> list = new ArrayList<>();
        File file = new File("src/Y2019/M12_/D12_6z/反射和IO_1$/dataAnimal.txt");
        List<String> strings = FileUtils.readLines(file);
        for (int i = 0; i < strings.size(); i++) {
            String[] s = strings.get(i).split(" ");
            Class<?> aClass = Class.forName(s[0]);
            if (s[0].contains("Cat")){
                Object o = aClass.getConstructor(String.class, String.class, String.class).newInstance(s[1].split("=")[1], s[2].split("=")[1], s[3].split("=")[1]);
                list.add((Animal) o);
            }
            if (s[0].contains("Dog")){
                Object o = aClass.getConstructor(String.class, String.class, int.class).newInstance(s[1].split("=")[1], s[2].split("=")[1], Integer.parseInt(s[3].split("=")[1]));
                list.add((Animal) o);
            }
        }
        return list;
    }
}
